package com.qf.controller.business;

import com.github.pagehelper.PageInfo;
import com.qf.entitys.Business;
import com.qf.entitys.Coupon;
import com.qf.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/business/coupon")
public class CouponController {
    @Resource
    private OrderService orderService;
    @Resource
    private MenuItemService menuItemService;
    @Resource
    private CouponService couponService;
    @Resource
    private MenuItemSpecificationsService specService;
    @Resource
    private ComorderService comorderService;
    @Resource
    private UserCouponService userCouponService;
    @Resource
    private ShipAddressService shipAddressService;
    @GetMapping("/add")
    public String add(HttpServletRequest request){
        return "business/couponadd";
    }
    @PostMapping("/add")
    public String add(Coupon coupon,HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        coupon.setBid(business.getBid());
        coupon.setStatus(0);
        couponService.insertSelective(coupon);
        return "redirect:list?pageindex=1";
    }
    @GetMapping("/list")
    public String list(Integer pageindex, HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        PageInfo<Coupon> pageInfo = couponService.selectAllByBidCoupons(business.getBid(), pageindex);
        request.setAttribute("pageinfo",pageInfo);
        return "business/couponlist";
    }
    @GetMapping("/query")
    public String query(Integer pageindex,String name, HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        PageInfo<Coupon> pageInfo = couponService.selectAllByBidCouponsAndCouponName(business.getBid(), pageindex,name);
        request.setAttribute("pageinfo",pageInfo);
        return "business/couponlist";
    }
    @GetMapping("/update")
    public String update(Integer id,HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        Coupon coupon = couponService.selectByPrimaryKey(id);
        if(coupon.getStatus()==0){
            coupon.setStatus(1);
            couponService.updateByPrimaryKeySelective(coupon);
        }else if(coupon.getStatus()==1){
            coupon.setStatus(0);
            couponService.updateByPrimaryKeySelective(coupon);
        }
        return "redirect:list?pageindex=1";
    }
    @GetMapping("/del")
    public String del(Integer id,HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        Coupon coupon = couponService.selectByPrimaryKey(id);
        coupon.setStatus(2);
        couponService.updateByPrimaryKeySelective(coupon);
        return "redirect:list?pageindex=1";
    }
}
